<template>
  <div class="task-history">
    <el-table :data="tableData" border style="width: 100%" max-height="650" height="650" v-loading="loading">
      <el-table-column prop="processDefinitionDescription" label="流程名称" />
      <el-table-column prop="startTime" label="开始时间" />
      <el-table-column prop="endTime" label="结束时间" />
      <el-table-column prop="durationInMillis" label="耗时(毫秒)" />
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="text" size="small"
                     @click="openFlow(scope.row)"
          >查看
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="total, prev, pager, next"
        :total="total"
        :current-page="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
  <TaskDetail :visibleProp="visibleProp"
              task-id=""
              :historyProcessInstanceId="historyProcessInstanceId"
              @close="visibleProp=false"
              @handleOk="loadData"></TaskDetail>

</template>

<script>
import {getCurrentInstance, onMounted, reactive, toRefs} from "vue";
import TaskDetail from './Task-Detail'

export default {
  name: "Task-History",
  components: {TaskDetail},
  setup() {
    let state = reactive({
      tableData: [],
      loading: false,
      visibleProp: false,
      historyProcessInstanceId: '',
      total: 0,
      page: 1,
    })
    const {proxy} = getCurrentInstance();
    const handleSizeChange = () => {}
    const handleCurrentChange = (val) => {
      state.page = val;
      loadData();
    }
    const loadData = async () => {
      let params = {
        finished: true
      }
      state.loading = true
      let rs = await proxy.$http.post(`/flowableapi/query/historic-process-instances`, params, {
        params: {
          start: (state.page - 1) * 10,
          order: "desc",
          sort: "endTime"
        }
      })
      state.tableData = rs.data.data;
      state.tableData.forEach((e) => {
        if (e.startTime) {
          e.startTime = e.startTime.replace('T', ' ').replace('+', ' ')
        }
        if (e.endTime) {
          e.endTime = e.endTime.replace('T', ' ').replace('+', ' ')
        }
      })
      state.total = rs.data.total;
      state.loading = false
    }
    const openFlow = (row) => {
      state.historyProcessInstanceId = row.id;
      state.visibleProp = true;
    }
    onMounted(() => {
      loadData();
    })
    return {
      ...toRefs(state),
      openFlow,
      loadData,
      handleSizeChange,
      handleCurrentChange
    }
  }
}
</script>

<style scoped lang="less">
.task-history {
  height: 100%;
}
</style>
